package cn.edu.xust.controller;


import cn.edu.xust.Ro.MulHotelEleRo;
import cn.edu.xust.Ro.MulHotelWtRo;
import cn.edu.xust.entity.DeviceEntity;
import cn.edu.xust.service.DeviceService;
import cn.edu.xust.service.ElecdataService;
import cn.edu.xust.service.WtdataService;
import cn.edu.xust.util.EmailSendUtil;
import cn.edu.xust.util.R;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/easycheck")
public class EasyCheckController {

    @Autowired
    private ElecdataService elecdatas;
    @Resource
    EmailSendUtil emailSendUtil;
    @Autowired
    private WtdataService wtdataService;    @Autowired
    private DeviceService deviceService;
    /**
     * 方便查询今日数量
     */
    @GetMapping("/checknum")
    public R checknum(){
        //检查水表数量
        String date= DateUtil.format(new Date(),"yyyy-MM-dd");
        String hotelid = "1002";


        StringBuffer stringBuffer = new StringBuffer();


        //正常数量
        List<DeviceEntity> eles = deviceService.getByHotelAndType(hotelid, "ele");
        List<DeviceEntity> hotwts = deviceService.getByHotelAndType(hotelid, "hotwt");
        List<DeviceEntity> coldwts = deviceService.getByHotelAndType(hotelid, "coldwt");


        List<MulHotelWtRo> coldwt = wtdataService.getByHType(hotelid, "coldwt", date);
        List<MulHotelWtRo> hotwt = wtdataService.getByHType(hotelid, "hotwt", date);
        List<MulHotelEleRo> ele = elecdatas.getByHType(hotelid, "ele", date);


        boolean needsend;

        stringBuffer.append("电表：配置"+eles.size()+"台，采集"+ele.size()+"台\n");
        stringBuffer.append("冷水：配置"+coldwts.size()+"台，采集"+coldwt.size()+"台\n");
        stringBuffer.append("热水：配置"+hotwts.size()+"台，采集"+hotwt.size()+"台\n");

        if (eles.size() != ele.size()) {
            needsend = true;
            stringBuffer.append("\n电表异常：").append(ele.size()).append("/").append(eles.size());

            // 提取ele中的房间号
            Set<String> eleRoomNames = new HashSet<>();
            for (MulHotelEleRo item : ele) {
                eleRoomNames.add(item.getRoomname());
            }

            // 提取eles中的房间号
            Set<String> elesRoomNames = new HashSet<>();
            for (DeviceEntity item : eles) {
                elesRoomNames.add(item.getRoomname());
            }

            // 找出eles中不存在于ele的房间号
            elesRoomNames.removeAll(eleRoomNames);


            stringBuffer.append("\n未匹配房间:");
            // 根据这些房间号，从eles中找到对应的DeviceEntity对象
            for (DeviceEntity item : eles) {
                if (elesRoomNames.contains(item.getRoomname())) {
                    stringBuffer.append(item.getRoomname()).append(":").append(item.getName()).append("\n");
                }
            }
        }


        if (coldwt.size() != coldwts.size()) {
            needsend = true;
            stringBuffer.append("\n冷水异常：").append(ele.size()).append("/").append(eles.size());


            // 提取ele中的房间号
            Set<String> eleRoomNames = new HashSet<>();
            for (MulHotelWtRo item : coldwt) {
                eleRoomNames.add(item.getRoomname());
            }

            // 提取eles中的房间号
            Set<String> elesRoomNames = new HashSet<>();
            for (DeviceEntity item : coldwts) {
                elesRoomNames.add(item.getRoomname());
            }

            // 找出eles中不存在于ele的房间号
            elesRoomNames.removeAll(eleRoomNames);


            stringBuffer.append("\n未匹配房间:");
            // 根据这些房间号，从eles中找到对应的DeviceEntity对象
            for (DeviceEntity item : coldwts) {
                if (elesRoomNames.contains(item.getRoomname())) {
                    stringBuffer.append(item.getRoomname()).append(":").append(item.getName()).append("\n");
                }
            }

        }


        if (hotwt.size() != hotwts.size()) {
            needsend = true;
            stringBuffer.append("\n热水异常：").append(ele.size()).append("/").append(eles.size());

            // 提取ele中的房间号
            Set<String> eleRoomNames = new HashSet<>();
            for (MulHotelWtRo item : hotwt) {
                eleRoomNames.add(item.getRoomname());
            }

            // 提取eles中的房间号
            Set<String> elesRoomNames = new HashSet<>();
            for (DeviceEntity item : hotwts) {
                elesRoomNames.add(item.getRoomname());
            }

            // 找出eles中不存在于ele的房间号
            elesRoomNames.removeAll(eleRoomNames);

            stringBuffer.append("\n未匹配房间:");
            // 根据这些房间号，从eles中找到对应的DeviceEntity对象
            for (DeviceEntity item : hotwts) {
                if (elesRoomNames.contains(item.getRoomname())) {
                    stringBuffer.append(item.getRoomname()).append(":").append(item.getName()).append("\n");
                }
            }
        }



    return R.ok().put("data",String.valueOf(stringBuffer));

    }
}
